package com.clothe.service;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import com.clothe.dao.IUserInfoDAO;
import com.clothe.dao.impl.UserInfoDAOImpl;
import com.clothe.database.DataBase;
import com.clothe.dto.UserInfo;

public class UserInfoService {

	IUserInfoDAO userInfoDAOImpl = new UserInfoDAOImpl();

	public boolean save(UserInfo userInfo) throws Exception {
		Connection connection = null;
		boolean flag = false;
		try {
			connection = DataBase.getConn();
			userInfoDAOImpl.setConn(connection);
			flag = userInfoDAOImpl.save(userInfo);
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);

		}
		return flag;

	}

	public boolean delete(int id) throws Exception {
		Connection connection = null;
		boolean flag = false;
		try {
			connection = DataBase.getConn();
			userInfoDAOImpl.setConn(connection);
			flag = userInfoDAOImpl.delete(id);
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);

		}
		return flag;

	}

	public boolean update(UserInfo userInfo) throws Exception {
		Connection connection = null;
		boolean flag = false;
		try {
			connection = DataBase.getConn();
			userInfoDAOImpl.setConn(connection);
			flag = userInfoDAOImpl.update(userInfo);
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);

		}
		return flag;

	}

	public UserInfo get(int id) throws Exception {
		Connection connection = null;
		UserInfo userInfo = null;
		try {
			userInfo = new UserInfo();
			connection = DataBase.getConn();
			userInfoDAOImpl.setConn(connection);
			userInfo = userInfoDAOImpl.get(id);
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);

		}
		return userInfo;

	}

	public List<UserInfo> list(int pageSize, int pageNo, String queryString)
			throws Exception {
		Connection connection = null;
		List<UserInfo> userInfoList = null;
		try {
			userInfoList = new ArrayList<UserInfo>();
			connection = DataBase.getConn();
			userInfoDAOImpl.setConn(connection);
			userInfoList = userInfoDAOImpl.list(pageSize, pageNo, queryString);
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);

		}
		return userInfoList;
	}

	public UserInfo login(String account, String password) throws Exception {
		Connection connection = null;
		UserInfo userInfo = null;
		try {
			userInfo = new UserInfo();
			connection = DataBase.getConn();
			userInfoDAOImpl.setConn(connection);
			userInfo = userInfoDAOImpl.login(account, password);

			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;

		} finally {
			DataBase.releaseConnection(connection);
		}

		return userInfo;

	}

	public int getCount(String queryString) throws Exception {
		int totalpage = 0;
		Connection connection = null;
		try {
			connection = DataBase.getConn();
			userInfoDAOImpl.setConn(connection);
			totalpage = userInfoDAOImpl.getCount(queryString);
			DataBase.commit();
		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);
		}

		return totalpage;

	}
	
	public boolean updatePsw(String newpsw,UserInfo userInfo) throws Exception{
		Connection connection = null;
		boolean flag = false;
		try {
			connection = DataBase.getConn();
			userInfoDAOImpl.setConn(connection);
			flag = userInfoDAOImpl.updatePsw(newpsw,userInfo);
			DataBase.commit();
			
		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		}
		finally{
			DataBase.releaseConnection(connection);
		}
		return flag;
		
	}

}
